package com.example.uCenter.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.uCenter.admin.entity.AdminUser;
import com.example.uCenter.admin.mapper.AdminUserMapper;
import com.example.uCenter.admin.service.AdminUserService;
import com.example.uCenter.common.Code;
import com.example.uCenter.common.Paging;
import com.example.uCenter.common.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 服务实现类
 *
 * @author xxs
 * @since 2022-08-27 08:20:38
 */
@Service
@Transactional
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {

    @Override
    public Result findAdminNameAndAdminPwd(String adminName, String adminPwd) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("admin_name", adminName);
        qw.eq("admin_pwd", adminPwd);
        AdminUser adminUser = getBaseMapper().selectOne(qw);
        Result result = new Result(false, Code.GET_ERROR, "查询数据失败");
        if (Objects.nonNull(adminUser) && adminUser.getAdminValid() != null && adminUser.getAdminState() != null) {
            Integer valid = adminUser.getAdminValid();
            Integer state = adminUser.getAdminState();
            if (valid == 0 && state == 1) {
                result.setData(adminUser);
                result.setCode(Code.GET_OK);
                result.setMsg("查询数据成功");
            }
        }
        return result;
    }

    /**
     * 分页查询
     * hhl
     * @param current 当前页
     * @param size 每页大小
     * @return 类型Result 查询成功返回20091返回单表中所有当前页的结果集、总页数、总记录数、当前页、每页大小、提示语句
     */
    @Override
    public Result findPaging(Integer current, Integer size) {
        IPage<AdminUser> page = new Page<>(current, size);
        getBaseMapper().selectPage(page,null);
        long pages = page.getPages();
        List<AdminUser> records = page.getRecords();
        System.out.println("测试点一");
        long total = page.getTotal();
        Result result = new Result();
        Paging paging = new Paging(pages, records, total,current,size);
        result.setCode(Code.GET_ALL_OK);
        result.setData(paging);
        result.setMsg("共"+page.getPages()+"页、"+page.getTotal()+"条记录，第" + current + "页查询成功，还剩下" + (page.getPages() - current) + "页");
        System.out.println("测试点二");
        return result;
    }
}
